package cn.uikit.demo.recyclerview

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import cn.uikit.demo.databinding.ActivityRecyclerviewTestBinding
import cn.uikit.demo.recyclerview.layoutManager.CustomLMFragment
import sivin.uikit.extend.jumpToFragment
import sivin.uikit.extend.setOnSafeClickListener
import sivin.uikit.framework.BindingActivity

class RvTestActivity : BindingActivity<ActivityRecyclerviewTestBinding>(
    ActivityRecyclerviewTestBinding::inflate
) {

    companion object {
        fun launch(context: Context) {
            val startIntent = Intent(context, RvTestActivity::class.java)
            context.startActivity(startIntent)
        }
    }

    override fun onViewCreate(savedInstanceState: Bundle?) {
        super.onViewCreate(savedInstanceState)
        initView()
        initEvent()
    }

    private fun initEvent() {
        val callback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (supportFragmentManager.backStackEntryCount > 0) {
                    supportFragmentManager.popBackStack()
                } else {
                    isEnabled = false
                    onBackPressedDispatcher.onBackPressed()
                }
            }
        }
        onBackPressedDispatcher.addCallback(this, callback)
    }

    private fun initView() {
        binding.gridStyle.setOnClickListener {
            jumpToFragment(binding.fragmentContainer.id, GridFragment(), "gridRvFragment")
        }

        binding.customLayoutManager.setOnSafeClickListener {
            jumpToFragment(binding.fragmentContainer.id, CustomLMFragment(), "CustomLMFragment")
        }
    }
}